// cd /projects/hsieh_project/proj_201809/code_2_202011/
// qstata mktsize_d_ind_top.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/mktsize_d_ind_top_log, replace text

clear all
cd /projects/

di "$S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/mktsize_d_ind_top/"
noi capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_reg.do"

global ds_ind "${dir_data}/ind_sum_all"

global lvmkt = "msa1983 fips zipcode"
local lvmkt = "msa1983 fips zipcode"

tempfile ds_temp
global ds_temp = "`ds_temp'"

/*----------------------------------------------------------------------------
Author: Adarsh Kumar
Objective: Regression of:
	Y = Chg in Ln(avg mkt size of top 10% / avg mkt size of all firms) 
	X = Chg in Ln(Est / Firm for top 10%) 

Top firm is defined by # est 
*/

use ${dir_data}/mkt_ind_sum_mktsize, clear
keep if year == $year1 | year == $year2
drop mkts*
/* Note: mkt_msa1983 (same for fips, zip) = mean market size of firm in industry
	 mkt_msa1983_10 (same for fips, zip) = same as above but for top 10% (by est)

	 dropping mkts* to avoid confusion with mkt_*
	 
	 
Merge with mkt_ind_sum to get #est of all and top 10% firms (top by est) for each year-industry
*/
merge 1:1 year ch_ind using ${dir_data}/mkt_ind_sum, keepusing(n_est*)
keep if _merge == 3
drop _merge


foreach perc in `l_perc' {
	*Mean # Est of top 10% (and it's log)
	gen mu_mkt_est_d_`perc' = n_est_`perc' / n_ind_`perc'
	gen ln_mu_mkt_est_d_`perc' = ln(mu_mkt_est_d_`perc')
	replace ln_mu_mkt_est_d_`perc' = -ln_mu_mkt_est_d_`perc' if year == $year1
	
	foreach vmkt in `lvmkt' {
		* Ratio of: market size of top 10% / all (and it's log)
		gen mkt_size_r_`vmkt'_`perc' = mkt_`vmkt'_`perc' / mkt_`vmkt'
		gen ln_mkt_size_r_`vmkt'_`perc' = ln(mkt_size_r_`vmkt'_`perc')
		replace ln_mkt_size_r_`vmkt'_`perc' = -ln_mkt_size_r_`vmkt'_`perc' if year == $year1
	}
}

collapse (sum) ln_mu_mkt_est* ln_mkt_size_r_*, by(ch_ind)



foreach perc in `l_perc' {
	*Merge SV weights: 
	merge 1:1 ch_ind using "${dir_out}/sv_weights/sv_weights_1977_2013.dta", assert(match) nogen keepusing(w_sv* w_num*)

	*Regressions: 
	local x  = "ln_mu_mkt_est_d_`perc'"
	
	f_reg_create "fr_reg" "1"
	
	foreach vmkt in `lvmkt' {
		reg ln_mkt_size_r_`vmkt'_`perc' `x' [aw = w_sv_`perc']
		f_reg_append "fr_reg" "`x'"
	}
	
	*Save regs:
	f_reg_save "fr_reg" "${dir_outf}/reg_mkt_size_d_ln_est_top_`perc'"
}


capture noi log close
// End of do file
